<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="机构名称:" prop="tenantName" label-width="100px">
        <el-input
          v-model="queryParams.tenantName"
          placeholder="请输入机构名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="优惠券类型" prop="couponType" label-width="100px">
        <el-select v-model="queryParams.couponType" placeholder="请选择类型" clearable filterable>
          <el-option v-for="item in dict.type.coupon_type" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="优惠券名称:" prop="couponName" label-width="100px">
        <el-input
          v-model="queryParams.couponName"
          placeholder="请输入优惠券名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名称:" prop="userName" label-width="100px">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户手机号:" prop="userPhone" label-width="100px">
        <el-input
          v-model="queryParams.userPhone"
          placeholder="请输入用户手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-tabs :active-name="activeName" v-model="queryParams.couponRecordState" @tab-click="handleCouponRecordState">
      <el-tab-pane label="全部" name="0"></el-tab-pane>
      <el-tab-pane label="未使用" name="1"></el-tab-pane>
      <el-tab-pane label="已使用" name="2"></el-tab-pane>
      <el-tab-pane label="已过期" name="3"></el-tab-pane>
    </el-tabs>

    <el-table v-loading="loading" :data="recordList">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="机构名称" align="center" prop="tenantName"/>
      <el-table-column label="优惠价类型" align="center" prop="couponType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.coupon_type" :value="scope.row.couponType"/>
        </template>
      </el-table-column>
      <el-table-column label="优惠券名称" align="center" prop="couponName"/>
      <el-table-column label="劵码" align="center" prop="couponId"/>
      <el-table-column label="领取方式" align="center" prop="pickupMethodName"/>
      <el-table-column label="领取时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="优惠券面额" align="center" prop="couponDenomination"/>
      <el-table-column label="用户名称" align="center" prop="userName"/>
      <el-table-column label="用户手机号" align="center" prop="userPhone"/>
      <el-table-column label="状态" align="center" prop="couponRecordState">
        <template slot-scope="scope">
          {{ couponRecordStateMap.find(item => item.value === scope.row.couponRecordState).label }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="mini"
            icon="el-icon-info"
            @click="handleDetail(scope.row)">
            查看详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <template>
      <el-dialog
        title="详情"
        :visible.sync="open"
        width="1000px"
      >
        <div class="detail-content">
          <div class="section">
            <div class="section-title">基本信息</div>
            <div class="section-content">
              <div class="info-item">
                <span class="info-label">状态：</span>
                <span class="info-value">{{
                    detail.couponId && couponRecordStateMap.find(item => item.value === detail.couponRecordState).label
                  }}</span>
              </div>
              <div class="info-item"><span class="info-label">优惠券名称：</span><span
                class="info-value">{{ detail.couponName }}</span></div>
              <div class="info-item"><span class="info-label">优惠券面额：</span><span
                class="info-value">{{ detail.couponDenomination }}</span></div>
              <div class="info-item"><span class="info-label">劵码：</span><span class="info-value">{{
                  detail.couponId
                }}</span></div>
              <div class="info-item">
                <span class="info-label">优惠券类型：</span>
                <span class="info-value">
                  <dict-tag :options="dict.type.coupon_type" :value="detail.couponType"/>
                </span>

              </div>
              <div class="info-item"><span class="info-label">所属机构：</span><span class="info-value">{{ detail.tenantName }}</span></div>
            </div>
          </div>
          <div class="section">
            <div class="section-title">用户信息</div>
            <div class="section-content">
              <div class="info-item"><span class="info-label">用户名称：</span><span
                class="info-value">{{ detail.userName }}</span></div>
              <div class="info-item"><span class="info-label">手机号：</span><span class="info-value">{{
                  detail.userPhone
                }}</span></div>
              <div class="info-item"><span class="info-label">领取时间：</span><span
                class="info-value">{{ detail.createTime }}</span></div>
              <div class="info-item"><span class="info-label">领取方式：</span><span
                class="info-value">{{ detail.pickupMethodName }}</span></div>
              <div class="info-item" v-if="detail.pickupMethod === '3'"><span class="info-label">核销码：</span><span
                class="info-value">{{ detail.verificationCode }}</span></div>
              <div class="info-item"><span class="info-label">使用时间：</span><span
                class="info-value">{{ parseTime(detail.useTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span></div>
              <div class="info-item"><span class="info-label">使用房源：</span><span
                class="info-value">{{ detail.useHouse }}</span></div>
              <div class="info-item"><span class="info-label">使用单号：</span><span
                class="info-value">{{ detail.useOrderId }}</span></div>
            </div>
          </div>
        </div>
        <span slot="footer" class="dialog-footer">
      <el-button @click="open = false">关闭</el-button>
    </span>
      </el-dialog>
    </template>


  </div>
</template>

<script>
import {getRecord, listRecord} from "@/api/marketing/record";
import {parseTime} from "../../../utils/ruoyi";

export default {
  name: "Record",
  dicts: ['coupon_type'],
  data() {
    return {
      activeName: "0",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 优惠卷领取记录信息表格数据
      recordList: [],
      detail: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      couponRecordStateMap: [
        {label: '未使用', value: '1'},
        {label: '已使用', value: '2'},
        {label: '已过期', value: '3'},
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        couponId: null,
        couponName: null,
        couponType: null,
        couponRecordState: null,
        couponDenomination: null,
        couponThreshold: null,
        couponLabel: null,
        activityStartTime: null,
        activityEndTime: null,
        validityPeriodType: null,
        applyScope: null,
        cityCode: null,
        pickupMethod: null,
        pickupMethodName: null,
        superpositionType: null,
        refundLapsedType: null,
        verificationCode: null,
        couponInstruction: null,
        userId: null,
        userName: null,
        userPhone: null,
        operatorId: null,
        tenantId: null,
        useOrderId: null,
        useHouse: null,
        useTime: null,
        tenantName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        couponId: [
          {required: true, message: "优惠券id不能为空", trigger: "blur"}
        ],
        userId: [
          {required: true, message: "用户id不能为空", trigger: "blur"}
        ],
        tenantId: [
          {required: true, message: "租户id不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    parseTime,
    /** 查询优惠卷领取记录信息列表 */
    getList() {
      this.loading = true;
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        couponRecordId: null,
        couponId: null,
        couponName: null,
        couponType: null,
        couponRecordState: null,
        couponDenomination: null,
        couponThreshold: null,
        couponLabel: null,
        activityStartTime: null,
        activityEndTime: null,
        validityPeriodType: null,
        applyScope: null,
        cityCode: null,
        pickupMethod: null,
        pickupMethodName: null,
        superpositionType: null,
        refundLapsedType: null,
        verificationCode: null,
        couponInstruction: null,
        userId: null,
        userName: null,
        userPhone: null,
        createBy: null,
        updateBy: null,
        operatorId: null,
        createTime: null,
        updateTime: null,
        remark: null,
        tenantId: null,
        tenantName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleCouponRecordState(tab) {
      this.activeName = tab.name;
      if (tab.name === "0") {
        this.queryParams.couponRecordState = null;
      } else {
        this.queryParams.couponRecordState = tab.name;
      }
      this.getList();
    },
    handleDetail(row) {
      getRecord(row.couponRecordId).then(response => {
        this.detail = response.data;
        this.open = true;
        this.title = "详情";
      });
    }
  }
};
</script>
<style scoped lang="scss">
.detail-content {
  padding: 20px;
  font-size: 14px;
}

.section {
  margin-bottom: 20px;
  .section-content {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-gap: 20px;
  }
}

.section-title {
  font-weight: bold;
  margin-bottom: 30px;
  font-size: 16px;
  border-left: 3px solid #409eff;
  padding: 10px;
  background-color: #ecf8ff;
}

.info-item {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.info-label {
  /*font-weight: bold;*/
  width: 120px;
  display: inline-block;
  text-align: right;
  margin-right: 10px;
}

.dialog-footer {
  text-align: right;
  padding: 0 20px 20px 0;
}
</style>
